import faker from 'faker'
import { Response, Request } from 'express'
import { IArticleData, IGameInfo, IUserInfo } from '../src/api/types'

const articleList: IUserInfo[] = []
const gameList: IGameInfo[] = []
const articleCount = 1
const mockFullContent =
  '<p>I am testing data, I am testing data.</p><p><img src="https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943"></p>'

// for (let i = 0; i < articleCount; i++) {
//   articleList.push({
//     id: i,
//     status: faker.random.arrayElement(['正常', '封禁']),
//     account: faker.lorem.findName(),
//     // abstractContent: faker.lorem.sentences(2),
//     // fullContent: mockFullContent,
//     // sourceURL: faker.internet.url(),
//     // imageURL: faker.image.imageUrl(),
//     // timestamp: faker.date.past().getTime(),
//     // platforms: [
//     //   faker.random.arrayElement(['a-platform', 'b-platform', 'c-platform'])
//     // ],
//     // disableComment: faker.datatype.boolean(),
//     // importance: faker.datatype.number({ min: 1, max: 3 }),
//     // author: faker.name.findName(),
//     // reviewer: faker.name.findName(),
//     // type: faker.random.arrayElement(['CN', 'US', 'JP', 'EU']),
//     // pageviews: faker.datatype.number({ min: 300, max: 500 })
//   })
// }
let formaDate = function (
  type?: 1 | 2 | 3 | 4 | 5 | 6 | 7,
  data?: Date | string | number
) {
  var dd = data ? new Date(data) : new Date()
  const y: any = dd.getFullYear() //年
  let m: any = dd.getMonth() + 1 //月
  m = m < 10 ? '0' + m : m
  let d: any = dd.getDate() //日
  d = d < 10 ? '0' + d : d
  let h: any = dd.getHours() //时
  h = h < 10 ? '0' + h : h
  let i: any = dd.getMinutes() //分
  i = i < 10 ? '0' + i : i
  let s: any = dd.getSeconds() //秒
  s = s < 10 ? '0' + s : s
  if (type == 1) {
    return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s
  } else if (type == 2) {
    return y + '-' + m + ' ' + d + ' ' + h + ':' + i
  } else if (type == 3) {
    return h + ':' + i + ':' + s
  } else if (type == 4) {
    return m + '-' + d + ' ' + h + ':' + i + ':' + s
  } else if (type == 5) {
    return y.toString() + m.toString() + d.toString()
  } else if (type == 6) {
    return (
      y.toString() +
      m.toString() +
      d.toString() +
      h.toString() +
      i.toString() +
      s.toString()
    )
  } else if (type == 7) {
    return m.toString() + '月' + d.toString() + '日' + h.toString() + '时'
  }
}
for (let i = 0; i < articleCount; i++) {
  articleList.push({
    id: i,
    status: faker.random.arrayElement(['正常', '封禁']),
    account: faker.name.findName(),
    commit: faker.address.streetAddress(),
    password: '1111',
    createTime: formaDate(1, faker.date.past().getTime()),
    endTime: formaDate(1, faker.date.past().getTime()),
    level: faker.random.arrayElement(['一级会员', '二级会员', '三级会员'])
  })
}
export const getArticles = (req: Request, res: Response) => {
  const { account, gameId, page = 1, limit = 20, sort } = req.query
  let list
  if (gameId) {
    list = articleList
  } else {
    list = gameList
  }
  let mockList
  if (gameId) {
    mockList = gameList as unknown as any
  } else {
    mockList = articleList.filter((item) => {
      if (account) {
        if (account == item.account) {
          return true
        }
      } else {
        return true
      }
    })
  }

  if (sort === '-id') {
    mockList = mockList.reverse()
  }

  const pageList = mockList.filter(
    (_: any, index: number) =>
      index < (limit as number) * (page as number) &&
      index >= (limit as number) * ((page as number) - 1)
  )

  return res.json({
    code: 20000,
    data: {
      total: mockList.length,
      items: pageList
    }
  })
}
export const getArticle = (req: Request, res: Response) => {
  const { id, gameId } = req.params
  for (const article of articleList) {
    if (article.id.toString() === id) {
      return res.json({
        code: 20000,
        data: {
          article
        }
      })
    }
  }
  return res.json({
    code: 70001,
    message: 'Article not found'
  })
}
export const createArticle = (req: Request, res: Response) => {
  const { article } = req.body
  const { gameId } = article
  gameId ? gameList.push(article) : articleList.push(article)
  return res.json({
    code: 20000,
    data: {
      article
    }
  })
}

export const updateArticle = (req: Request, res: Response) => {
  const { id } = req.params
  const { article } = req.body
  const { gameId } = article
  const list = gameId ? gameList : articleList
  for (const v of list) {
    if (v.id.toString() === id) {
      return res.json({
        code: 20000,
        data: {
          article
        }
      })
    }
  }
  return res.json({
    code: 70001,
    message: 'Article not found'
  })
}

export const deleteArticle = (req: Request, res: Response) => {
  return res.json({
    code: 20000
  })
}

export const getPageviews = (req: Request, res: Response) => {
  return res.json({
    code: 20000,
    data: {
      pageviews: [
        { key: 'PC', pageviews: 1024 },
        { key: 'Mobile', pageviews: 1024 },
        { key: 'iOS', pageviews: 1024 },
        { key: 'Android', pageviews: 1024 }
      ]
    }
  })
}
